package cxm.lib.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import org.apache.http.conn.util.InetAddressUtils;

public class NetworkHelper {

	public static String getLocalIpAddress()
	{
		String networkAddress = null;
		try {
			for (Enumeration<NetworkInterface> en =
					NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress() &&
							InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
						networkAddress = inetAddress.getHostAddress().toString();
						break;
					}
				}
			}
		} catch (Exception e) {
			MyLog.Log(MyLog.Level.Error, "Cannot get ip: " + e);
		}

		return networkAddress;
	}
}
